package com.samsung.sds.test.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;

public class JSPConfiguration extends SourceViewerConfiguration {
	private JAVASCRIPTDoubleClickStrategy doubleClickStrategy;
	private JAVASCRIPTScanner scanner;
	private ColorManager colorManager;

	public JSPConfiguration(ColorManager colorManager) {
		this.colorManager = colorManager;
	}
	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] {
				IDocument.DEFAULT_CONTENT_TYPE, 
				JAVASCRIPTPartitionScanner.JS_COMMENT,
				JAVASCRIPTPartitionScanner.JS_KEYWORD,
				HTMLPartitionScanner.JAVASCRIPT 
		};

	}

	public ITextDoubleClickStrategy getDoubleClickStrategy(
			ISourceViewer sourceViewer,
			String contentType) {
		if (doubleClickStrategy == null)
			doubleClickStrategy = new JAVASCRIPTDoubleClickStrategy();
		return doubleClickStrategy;
	}

	protected JAVASCRIPTScanner getJAVASCRIPTScanner() {
		System.out.println("JAVASCRIPTSCANNER.getJAVASCRIPTPartitionScanner called");
		if (scanner == null) {
			scanner = new JAVASCRIPTScanner(colorManager);
			scanner.setDefaultReturnToken(
					new Token(
							new TextAttribute(
									colorManager.getColor(IJAVASCRIPTColorConstants.DEFAULT))));
		}
		return scanner;
	}

	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		System.out.println("JSPConfiguration.getPresentationReconciler called");
		final PresentationReconciler reconciler = new PresentationReconciler ();

		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getJAVASCRIPTScanner()); 
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); 
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		return reconciler;
	}

	// Content Assist
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
		System.out.println("JSPConfiguration.getContentAssistant called!!!");
		final ContentAssistant assistant = new ContentAssistant(); 
		assistant.setContentAssistProcessor( new MyCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE); 
		assistant.setContentAssistProcessor( new MyCompletionProcessor(), HTMLPartitionScanner.JAVASCRIPT); 

		assistant.enableAutoActivation(true);
		assistant.setAutoActivationDelay(500);
		assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);

		return assistant;
	}
	
}
